<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="账号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stuAccount">
              <a-input v-model="model.stuAccount" placeholder="请输入账号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="昵称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stuUsername">
              <a-input v-model="model.stuUsername" placeholder="请输入昵称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="头像" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stuPhoto">
              <j-image-upload isMultiple  v-model="model.stuPhoto" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="真实姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stuName">
              <a-input v-model="model.stuName" placeholder="请输入真实姓名"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="现居地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="addressId">
             <j-area-linkage type="cascader" v-model="model.addressId" placeholder="请输入省市区"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="手机号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stuPhone">
              <a-input v-model="model.stuPhone" placeholder="请输入手机号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学历" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="educationId">
              <j-dict-select-tag type="list" v-model="model.educationId" dictCode="EDUCATION,EDU_NAME,EDU_ID" placeholder="请选择学历" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="专业" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="majorId">
              <j-dict-select-tag type="list" v-model="model.majorId" dictCode="MAJOR,MAJOR_NAME,MAJOR_ID" placeholder="请选择专业" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="身份证" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stuIdcard">
              <a-input v-model="model.stuIdcard" placeholder="请输入身份证"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stuSex">
              <j-dict-select-tag type="radio" v-model="model.stuSex" dictCode="sex" placeholder="请选择性别" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'StuForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
            stuStatusId:"0",
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          applyName: [
            { required: true, message: '请输入学生姓名!'},
          ],
          majorId: [
            { required: true, message: '请输入报名专业!'},
          ],
          applyTime: [
            { required: true, message: '请输入报名时间!'},
          ],
          applyPhone: [
            { required: true, message: '请输入手机号!'},
            { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'},
          ],
          applySex: [
            { required: true, message: '请输入性别!'},
          ],
          educationId: [
            { required: true, message: '请输入学历编号!'},
          ],
          applyOldMajor: [
            { required: true, message: '请输入所学专业!'},
          ],
          applyEmail: [
            { required: true, message: '请输入电子邮箱!'},
            { pattern: /^([\w]+\.*)([\w]+)@[\w]+\.\w{3}(\.\w{2}|)$/, message: '请输入正确的电子邮件!'},
          ],
          applyQqnumber: [
            { required: true, message: '请输入QQ号码!'},
            { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},
          ],
          applyIdcard: [
            { required: true, message: '请输入身份证!'},
            { pattern: '[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]', message: '不符合校验规则!'},
          ],
          applyAddress: [
            { required: true, message: '请输入家庭住址!'},
          ],
          applyFamilyPhone: [
            { required: true, message: '请输入监护人电话!'},
            { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'},
          ],
        },
        url: {
          add: "/stu/stu/add",
          edit: "/stu/stu/edit",
          queryById: "/stu/stu/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>